package br.com.news.web.service;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.core.MediaType;

import br.com.news.utils.database.beans.AcessoNoticiaBean;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;

public class AcessoNoticiaService {

	public List<AcessoNoticiaBean> maisLidasDaSemana(int qntNoticias,int idCategoria) {
	    Client c = Client.create();
	    WebResource wr = c.resource("http://localhost:8080/news-web-service-rest/acessoNoticia/maisLidasDaSemana/"+qntNoticias+"/"+idCategoria);
	    String json = wr.get(String.class);
	    Gson gson = new Gson();
	    List<AcessoNoticiaBean> acessoNoticias = gson.fromJson(json, new TypeToken<ArrayList<AcessoNoticiaBean>>(){}.getType());
	    
	    for (AcessoNoticiaBean acesso : acessoNoticias) {
	    	String[] teste = acesso.getNoticia().getTitulo().trim().split("               ");
	    	if (teste.length > 1) {
	    		
	    	
	    	acesso.getNoticia().setSubTitulo(acesso.getNoticia().getTitulo().trim().split("               ")[1]);
	    	acesso.getNoticia().setTitulo(acesso.getNoticia().getTitulo().trim().split("               ")[0]);
	    	}
	    }
	    
	    return acessoNoticias;
	}
	
	
	public void save(AcessoNoticiaBean example) {
		 
			  Gson g = new Gson();
			  String json = g.toJson(example);
	 
			    Client c = Client.create();
			    c.resource("http://localhost:8080/news-web-service-rest/acessoNoticia/insert").type(MediaType.APPLICATION_JSON).put(json);
		 
		}
	
}
